﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.MEP.Electric.CableLay
{
	public class CableTrayLookUp
	{
		public CableTrayLookUp(UIDocument uiDoc, string layPathIds)
		{
			this.m_uiDoc = uiDoc;
			this.m_doc = uiDoc.ActiveView.Document;
			this.SplitLayPath(layPathIds);
		}

		public void LookUp()
		{
			if (this.m_doc == null || this.m_lstEntId == null || this.m_lstEntId.Count <= 0)
			{
				return;
			}
			List<Element> list = new List<Element>();
			using (List<ElementId>.Enumerator enumerator = this.m_lstEntId.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ElementId entId = enumerator.Current;
					if (!(ElementId.InvalidElementId == entId) && !list.Exists((Element item) => item.Id == entId))
					{
						Element element = this.m_doc.GetElement(entId);
						if (element != null)
						{
							list.Add(element);
							List<Element> connectElements = this.GetConnectElements(element);
							if (connectElements != null && connectElements.Count > 0)
							{
								using (List<Element>.Enumerator enumerator2 = connectElements.GetEnumerator())
								{
									while (enumerator2.MoveNext())
									{
										Element connEnt = enumerator2.Current;
										if (!list.Exists((Element item) => item.Id == connEnt.Id))
										{
											list.Add(connEnt);
										}
									}
								}
							}
						}
					}
				}
			}
			list = this.FilterEndFitting(list);
			View view = CableLayRevitCommon.Get3DView(this.m_uiDoc);
			if (view == null)
			{
				return;
			}
			this.m_uiDoc.ActiveView = view;
			CableLayRevitCommon.HighlightElement(this.m_uiDoc, list);
		}

		private void SplitLayPath(string layPath)
		{
			if (string.IsNullOrWhiteSpace(layPath))
			{
				return;
			}
			this.m_lstEntId = new List<ElementId>();
			string[] array = layPath.Trim().Split(CableLayCommon.m_sp, StringSplitOptions.RemoveEmptyEntries);
			for (int i = 0; i < array.Length; i++)
			{
				ElementId item = new ElementId(Convert.ToInt32(array[i]));
				this.m_lstEntId.Add(item);
			}
		}

		private List<Element> GetConnectElements(Element ent)
		{
			if (ent == null)
			{
				return null;
			}
			MEPCurve mepcurve = ent as MEPCurve;
			if (mepcurve == null)
			{
				return null;
			}
			ConnectorSet connectors = mepcurve.ConnectorManager.Connectors;
			if (connectors.Size <= 0)
			{
				return null;
			}
			List<Element> list = new List<Element>();
			foreach (object obj in connectors)
			{
				Connector connector = (Connector)obj;
				if (connector.IsConnected)
				{
					foreach (object obj2 in connector.AllRefs)
					{
						Connector connector2 = (Connector)obj2;
						if (connector2.IsConnected)
						{
							Element owner = connector2.Owner;
							if (owner != null && owner is FamilyInstance)
							{
								list.Add(owner);
							}
						}
					}
				}
			}
			return list;
		}

		private List<Element> FilterEndFitting(List<Element> entSet)
		{
			List<Element> list = new List<Element>();
			if (entSet == null || entSet.Count <= 0)
			{
				return list;
			}
			foreach (Element element in entSet)
			{
				if (element != null)
				{
					if (!(element is FamilyInstance))
					{
						list.Add(element);
					}
					else
					{
						int integerValue = element.Category.Id.IntegerValue;
						if ((integerValue != -2008126 && integerValue != -2008128) || !this.IsEndFitting(element, entSet))
						{
							list.Add(element);
						}
					}
				}
			}
			return list;
		}

		private bool IsEndFitting(Element ent, List<Element> entSet)
		{
			if (ent == null || entSet == null || entSet.Count <= 0)
			{
				return false;
			}
			FamilyInstance familyInstance = ent as FamilyInstance;
			if (familyInstance == null)
			{
				return false;
			}
			if (familyInstance.MEPModel == null || familyInstance.MEPModel.ConnectorManager == null)
			{
				return true;
			}
			ConnectorSet connectors = familyInstance.MEPModel.ConnectorManager.Connectors;
			if (connectors == null || connectors.Size <= 0)
			{
				return true;
			}
			int num = 0;
			foreach (object obj in connectors)
			{
				Connector connector = (Connector)obj;
				if (connector.IsConnected)
				{
					foreach (object obj2 in connector.AllRefs)
					{
						Connector connector2 = (Connector)obj2;
						if (connector2.IsConnected)
						{
							Element refEnt = connector2.Owner;
							if (refEnt != null && entSet.Exists((Element item) => item.Id == refEnt.Id))
							{
								num++;
							}
						}
					}
				}
			}
			return num <= 1;
		}

		private UIDocument m_uiDoc;

		private Document m_doc;

		private List<ElementId> m_lstEntId;
	}
}
